import 'package:dio/dio.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/utils/lf_logger.dart';

class ExceptionHandle {
  static const int success = 200;

  static const int net_error = 1000;

  static final Map<int, NetError> _errorMap = <int, NetError>{
    net_error:
        NetError(net_error, 'Network anomaly, please check your network'),
  };

  static NetError handleException(dynamic error) {
    if (error is NetError) {
      return error;
    } else if (error is DioException) {
      return _errorMap[net_error]!;

      /// dio 错误一律返回网络异常
    } else {
      return _handleException(error);
    }
  }

  static NetError _handleException(dynamic error) {
    lfLogger.e(error);

    ///错误一律返回网络异常
    return _errorMap[net_error]!;
  }
}

class NetError {
  NetError(this.code, this.msg);
  int code;
  String msg;
}

isTokenError(int code) {
  if (code == codeTokenInvalid ||
      code == codeErrorLoginFirst2 ||
      code == codeLoginFirst ||
      code == codeSignError ||
      code == codeSignError2 ||
      code == codeTokenError ||
      code == codeAuthorizationExpired) {
    return true;
  }
  return false;
}
